package com.arvin.service.serviceImpl;

import com.arvin.mapper.NoticeMapper;
import com.arvin.pojo.Notice;
import com.arvin.pojo.PageBean;
import com.arvin.service.NoticeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.Not;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 获取公告信息
     * @param noticeTitle
     * @param page
     * @param pageSize
     * @return
     */
    public PageBean getNoticeInfo(String noticeTitle,String noticeType, String noticeState,
                                  Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Notice> noticeList = noticeMapper.getNoticeInfo(noticeTitle, noticeType, noticeState);
        Page<Notice> noticePage = (Page<Notice>) noticeList;
        PageBean pageBean = new PageBean(noticePage.getTotal(),noticePage.getResult());
        return pageBean;
    }

    /**
     * 根据id查询公告
     * @param noticeId
     * @return
     */
    @Override
    public Notice getNoticeInfoById(Integer noticeId) {
        return noticeMapper.getNoticeInfoById(noticeId);
    }

    /**
     * 发布公告
     * @param notice
     */
    public void publishNotice(Notice notice) {
        notice.setNoticeState("使用中");
        noticeMapper.publishNotice(notice);
    }

    /**
     * 修改公告
     * @param notice
     */
    public void updatePublishNotice(Notice notice) {
        noticeMapper.updatePublishNotice(notice);
    }

    /**
     * 删除公告
     * @param noticeId
     */
    @Override
    public void deleteNotice(Integer noticeId) {
        noticeMapper.deleteNotice(noticeId);
    }

    /**
     * 更新公告状态
     * @param notice
     */
    @Override
    public void updateNoticeState(Notice notice) {
        notice.setUpdateTime(LocalDateTime.now());
        noticeMapper.updatePublishNotice(notice);
    }

    /**
     * 获取全部公告信息
     * @return
     */
    @Override
    public List<Notice> getAllNotice(String noticeState) {
        return noticeMapper.selectAllNotice(noticeState);
    }
}
